﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;


namespace ImageDateTaken
{
    class ImageDateTakenFromExif
    {
        const int EXIF_DATE_TAKEN_VALUE = 0x9003;  //36867;

        static void Main(string[] args)
        {
            //String file = @"C:\Resources\1.jpg";
            //string s = @"C:\Documents and Settings\andyk.RWN\Documents\My Dropbox\Camera Uploads\";
            if (args.Length == 1)
            {
                int iTotalFiles = 0;
                int iTotalFilesProcessed = 0;
                // need to check errors here
                string[] filePaths = Directory.GetFiles(args[0], "*.jpg");
                Console.WriteLine("Processing {0} files", filePaths.Length);
                foreach (string file in Directory.GetFiles(args[0], "*.jpg"))
                {
                    iTotalFiles++;
                    Console.Write("{0}/{1}. Getting exif date from \"{2}\": ", iTotalFiles, filePaths.Length,Path.GetFileName(file));
                    DateTime? dt = DateTaken(file);
                    if (dt != null)
                    {
                        Console.WriteLine("{0}. Processing", dt);
                        if (File.GetCreationTime(file) == dt)
                        { 
                            Console.WriteLine("SKIPPING. Creation Date and exif taken date are equal {0}.", dt);
                        }
                        else
                        {
                            ChangeCreatedDate(file, (DateTime)dt);
                            Console.WriteLine("PROCESSED. Changed Creation Date exif taken date: {0}.", dt);
                            iTotalFilesProcessed++;
                        }
                    }
                    else
                    {
                        Console.WriteLine("SKIPPING. File not found.");
                    }

                }
                Console.WriteLine("DONE. Checked {0} files and processed successfuly {1} files",iTotalFiles,iTotalFilesProcessed);

            }
            else
            {
                Console.WriteLine("Wrong parameter. Please specify directory");
            }

        }

        public static DateTime? DateTaken(String imageFile)
        {
            DateTime? dt=null;
            try
            {
                Image getImage = Image.FromFile(imageFile);
                dt = DateTaken(getImage);
                getImage.Dispose();
                return dt;
            }
            catch
            {
                return null;
            }
        }

        public static DateTime? DateTaken(Image getImage)
        {
            DateTime? dt = null;
            try
            {


                // Check if a "Date Taken" exists
                if (!getImage.PropertyIdList.Contains(EXIF_DATE_TAKEN_VALUE))
                {
                    dt = null;
                }
                else
                {
                    dt = DateTime.ParseExact(Encoding.ASCII.GetString(getImage.GetPropertyItem(EXIF_DATE_TAKEN_VALUE).Value), "yyyy:MM:dd HH:mm:ss\0", null);
                }
            }
            catch
            {
                dt = null;
            }
            return dt;

        }

        public static Boolean ChangeCreatedDate(string imageFile, DateTime setTime)
        {
            try
            {
                File.SetCreationTime(imageFile, setTime);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /*public static Boolean ChangeCreatedDate(Image getImage, DateTime? setTime)
        {

            PropertyItem propItem = getImage.GetPropertyItem(EXIF_DATE_TAKEN_VALUE); ;
            propItem.Value=null;
            getImage.SetPropertyItem(propItem);

            return true;
        }*/
    }
}
